package ejb.manager;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import ejb.dao.NotificationDAO;
import ejb.entity.City;
import ejb.entity.DefaultNotification;
import ejb.entity.Notification;
import ejb.entity.Position;


@Stateless
public class DefaultNotificationManager implements NotificationManager {
	@EJB
	private NotificationDAO ndao;
	@EJB
	private PositionManager pm;
	@EJB
	private CityManager cm;
	@EJB
	private MailManager mm;
	
	private ResourceBundle msg;

	/* (non-Javadoc)
	 * @see ejb.manager.NotificationManager#addNotification(java.lang.String, int, int, int, int, ejb.entity.Position)
	 */
	@Override
	public Notification addNotification(String message, int type, int mintemp, int maxtemp, int minpressure, int maxpressure, int position_id, boolean one_time){
		Position position = pm.getPositionById(position_id);
		if(message == null) message = msg.getString("notificationMsg") + position.getName();
		Notification notification = new DefaultNotification(message, type, mintemp, maxtemp, minpressure, maxpressure, position, one_time);
		System.out.println(notification.getMessage());
		System.out.println(notification.getMinpressure());
		System.out.println(notification.getMintemperature());
		System.out.println(notification.getMaxpressure());
		System.out.println(notification.getMaxtemperature());
		System.out.println("Pozycja: "+notification.getPosition().getName());
		notification = ndao.create(notification);			
		return notification;			
	}
		
	/* (non-Javadoc)
	 * @see ejb.manager.NotificationManager#getPositionNotification(ejb.entity.Position)
	 */
	@Override
	public List<Notification> getPositionNotification(int position_id){
		Position position = pm.getPositionById(position_id);
		List<Notification> positionNotification = ndao.findByPosition(position);
		
		if(positionNotification == null) return null;
		else return positionNotification;			
	}
	
	/* (non-Javadoc)
	 * @see ejb.manager.NotificationManager#getPositionNotification(ejb.entity.Position)
	 */
	@Override
	public boolean sendNotification(List<City> oldcities){
		List<Notification> allNotifications = ndao.findAll();
		
		Iterator<Notification> it = allNotifications.iterator();
		while(it.hasNext()){			
			Notification notification = (Notification) it.next();
			Position position = notification.getPosition();
			int oldtemp = (int) cm.approxTemp(position.getPositionx(), position.getPositiony(), oldcities);
			int oldpressure = (int) cm.approxPress(position.getPositionx(), position.getPositiony(), oldcities);
			int oldconditions = (int) cm.approxConds(position.getPositionx(), position.getPositiony(), oldcities);
			
			int temp = (int) cm.approxTemp(position.getPositionx(), position.getPositiony());
			int pressure = (int) cm.approxPress(position.getPositionx(), position.getPositiony());
			int conditions = (int) cm.approxConds(position.getPositionx(), position.getPositiony());
			
			//System.out.println("TUTAJ: " + notification.getMaxpressure() + " as " + notification.getMinpressure());
			if(notification.isAlready_send()) continue;
			int i_nowa=0;
			if(temp>notification.getMintemperature() || notification.getMintemperature() == -65) i_nowa++;
			if(temp<notification.getMaxtemperature() || notification.getMaxtemperature() == -65) i_nowa++;
			if(pressure>notification.getMinpressure() || notification.getMinpressure() == -65) i_nowa++;
			if(pressure<notification.getMaxpressure() || notification.getMaxpressure() == -65) i_nowa++;
			if(conditions == notification.getType() || notification.getType() == -65) i_nowa++;
			
			int i_stara=0;
			if(oldtemp>notification.getMintemperature() || notification.getMintemperature() == -65) i_stara++;
			if(oldtemp<notification.getMaxtemperature() || notification.getMaxtemperature() == -65) i_stara++;
			if(oldpressure>notification.getMinpressure() || notification.getMinpressure() == -65) i_stara++;
			if(oldpressure<notification.getMaxpressure() || notification.getMaxpressure() == -65) i_stara++;
			if(oldconditions == notification.getType() || notification.getType() == -65) i_stara++;
			
			
			if((i_nowa == 5 && i_stara < 5) || (i_nowa == 5 && notification.getLast_send() == null)) {
				mm.send(notification.getPosition().getUser().getEmail(), notification.getMessage());
				Timestamp now = new Timestamp(Calendar.getInstance().getTimeInMillis());
				
				notification.setLast_send(now);
				notification.setAlready_send(true);
				if(notification.isOne_time()){
					System.out.println("JEDNORAZOWE");
					ndao.delete(notification);
				}
			} else if(i_nowa < 5 && i_stara == 5) {
				notification.setAlready_send(false);
			}

		}
	
		
		return true;
	}
	
	public boolean deleteNotification(int id){
		Notification n = ndao.findById(id);
		return ndao.delete(n);		
	}
	
}
